package com.Blog.www.service.Impl;

import com.Blog.www.dao.ColumnDao;
import com.Blog.www.dao.Impl.ColumnDaoImpl;
import com.Blog.www.po.Column;
import com.Blog.www.po.PageBean;
import com.Blog.www.service.ColumnService;
import com.Blog.www.utils.DataExamine;

import java.util.List;

/**
 * 实现类
 *
 * @author 君未洋
 */
public class ColumnServiceImpl implements ColumnService {
    private static ColumnDao dao = new ColumnDaoImpl();
    private static DataExamine date = new DataExamine();

    @Override
    public PageBean<Column> findColumnByPage(String getRows, String getCurrentPage, String prior) {
        //先校验下传入的数据
        PageBean<Column> pb = new PageBean<Column>();
        //设置参数

        if (getCurrentPage == null || "".equals(getCurrentPage) || date.numberExamine(getCurrentPage)) {
            getCurrentPage = "1";
        }

        if (getRows == null || "".equals(getRows) || date.numberExamine(getRows)) {
            getRows = "5";
        }
        int currentPage = Integer.parseInt(getCurrentPage);
        int rows = Integer.parseInt(getRows);
        if (currentPage <= 0) {
            currentPage = 1;
            //防止跳转到后面
        }

        if ("".equals(prior) || prior == null || date.numberExamine(prior)) {
            prior = "0";
        }
        //调用dao查询总记录
        //调用博客里的展示方法
        int totalCount = dao.getCount(prior);
        pb.setTotalCount(totalCount);
        //设置到pb中
        //5.计算总页码
        int totalPage = (totalCount % rows) == 0 ? (totalCount / rows) : (totalCount / rows + 1);
        pb.setTotalPage(totalPage);

        if (currentPage > totalPage) {
            currentPage = totalPage;
        }


        pb.setCurrentPage(currentPage);
        pb.setRows(rows);

        //这里是调用dao索引list集合
        int start = (currentPage - 1) * rows;
        List<Column> list = dao.findColumnList(start, rows, prior);
        pb.setList(list);

        return pb;
    }

    @Override
    public boolean addColumn(String id, String bid, String addColumn, String prior) {
        //稍微校验一下数据
        if ("".equals(addColumn) || addColumn == null || "".equals(prior) || prior == null || date.numberExamine(prior)) {
            if (id == null || "".equals(id) || bid == null || "".equals(bid) || date.numberExamine(id) || date.numberExamine(bid)) {
                return false;
            } else {
                if (dao.deleteColumn(bid)) {
                    return dao.addColumn(id, bid);
                } else {
                    return false;
                }
            }
        } else {
            if (bid == null || "".equals(bid) || date.numberExamine(bid)) {
                return false;
            } else {
                Column column = dao.findColumn(addColumn);
                if (column == null) {
                    if (dao.addBlogColumn(prior, addColumn)) {
                        column = dao.findColumn(addColumn);
                    } else {
                        System.out.println(3);
                        return false;
                    }
                }
                if (dao.deleteColumn(bid)) {
                    return dao.addColumn(column.getId() + "", bid);
                } else {
                    System.out.println(4);
                    return false;
                }
            }
        }
    }

    @Override
    public boolean deleteList(String[] columnIds) {
        //开始操作
        //虽然传进来一般没问题,但还是要基本校验一下
        if (columnIds == null || columnIds.length == 0) {
            return false;
        }
        for (String columnId : columnIds) {
            if (date.numberExamine(columnId)) {
                return false;
            }
        }
        boolean flag = false;
        for (String columnId : columnIds) {
            flag = dao.deleteBlogColumn(Integer.parseInt(columnId));
        }
        return flag;
    }
}
